<template>
	<el-dialog title="资产明细" width="500px" :visible.sync="detDialogVisible" @opened="openedDilog" :before-close="handleClose"
		 class="my-dialog-class" :close-on-click-modal="true">
		<el-form :model="dataForm" label-width="120px" ref="dataForm" :rules="dataFormRules" :size="global.gSize"
			label-position="right" :key="timer">
			<el-row>
				<el-col :span="24">
					<s-item-form :canSelect=editItem p="itemId" @change="itemChange" v-model="dataForm.itemId" />
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="设备编码" prop="code" style="text-align:left">
						{{ dataForm.code }}
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="规格型号" prop="gg" style="text-align:left">
						{{ dataForm.gg }}
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="资产数量" prop="countDb" style="text-align:left">
						<el-input type="text" v-model="dataForm.countDb" @keydown.enter.native="submitForm"
							v-if=editCount></el-input>
						<span v-else> {{ dataForm.countDb }} </span>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24" style="text-align:left">
					<s-dict-form :canSelect="false" :key="dataForm.unit" name="units" :value="dataForm.unit" :disabled=true
						v-model="dataForm.unit" />
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="批次号" prop="batchNo" style="text-align:left">
						<el-input type="text" v-model="dataForm.batchNo" v-if=editBatch></el-input>
						<span v-else> {{ dataForm.batchNo }} </span>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="盘库标题" style="text-align:left">

						<span> {{ pkTitle }} </span>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button :size="global.gSize" @click.native="handleClose()">{{ $t('action.cancel') }}</el-button>
			<el-button :size="global.gSize" type="primary" v-if="editCount || editItem || editBatch"
				@click.native="submitForm">{{ $t('action.submit') }}</el-button>
		</div>
	</el-dialog>
</template>

<script>
import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
import { getItem, dictsFormat } from "@/utils/myUtil"
import KtDeleteButton from "@/views/Core/KtDeleteButton"
import { validatorNumber } from "@/utils/validate"
import SDictForm from "@/components/Kj/SDictForm"
import SItemForm from "@/components/Kj/SItemForm"
export default {
	components: {
		KtButton,
		SDictForm, SItemForm
	},
	props: {

		detDialogVisible: false,

		editItem: {
			type: Boolean,
			default: false,
		},
		editCount: {
			type: Boolean,
			default: false,
		},
		editBatch: {
			type: Boolean,
			default: false,
		},
		id: 0,
		itemId: 0,
		batchNo: ""
	},
	data() {
		return {
			timer: new Date().getTime(), //声明timer  
			dataForm: {
				id: 0,
				batchNo: ""
			},
			pkTitle: ""
		}
	},
	computed: {
		dataFormRules() {
			const dataFormRules = {
				itemId: [{ required: true, message: "请选择物料", trigger: 'blur' }],
				countDb: [{ required: true, message: "请输入数量", trigger: 'blur' }, { validator: validatorNumber, trigger: 'blur' }]
			};
			return dataFormRules;
		}
	},
	methods: {
		openedDilog: function (data) {

			if (this.id > 0) {
				this.$api.coreStockDet.getStockDetail({ id: this.id }).then((res) => {
					if (res.data) {
						this.dataForm = res.data
						this.itemChange(this.dataForm.itemId);

						this.$api.pk.getPk({ id: this.dataForm.pkId }).then((res) => {
							if (res.data) {

								this.pkTitle = res.data.title;
							}


						})
					}
				})
			} else {
				if (this.itemId > 0) {
					this.dataForm.itemId = this.itemId;
					this.itemChange(this.dataForm.itemId);
				}
				this.dataForm.batchNo = this.batchNo;
			}

			this.timer = new Date().getTime();
		},
		submitForm: function () {
			this.$refs.dataForm.validate((valid) => {
				if (valid) {
					this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
						let params = Object.assign({}, this.dataForm)
						this.$emit('onSubmitForm', params)
					})
				}
			})
		},
		handleClose: function (det) {
			this.$emit('myClose', det)
		},

		getKey: function (arg) {
			return this.$t(arg)
		},
		itemChange: function (val) {
			var item = getItem(val)
			this.dataForm.code = item.c;
			this.dataForm.gg = item.g;
			this.dataForm.unit = item.u;
		}
	},

	mounted() {

	}
}
</script>

<style scoped></style>